package com.blue.validation.spi;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.blue.validation.IdList;

public class IdListValidator implements ConstraintValidator<IdList, String>
{
	private static Pattern pattern = Pattern.compile("^[+-]?\\d*[1-9]\\d*$");
	
	public IdListValidator()
	{
	}

	@Override
	public void initialize(IdList anno)
	{
	}

	@Override
	public boolean isValid(String text, ConstraintValidatorContext context)
	{
		if (text == null || text.isEmpty())
			return true;
		
		for (String strId : text.split(","))
		{
			Matcher matcher = pattern.matcher(strId.trim());
			if (!matcher.matches())
				return false;
		}
		
		return true;
	}
}
